package com.oystertech.sportcms.controller;

import cn.hutool.core.lang.Assert;
import com.oystertech.sportcms.common.annotation.PreventDuplicateSubmit;
import com.oystertech.sportcms.common.result.Result;
import com.oystertech.sportcms.common.util.SecurityUtils;
import com.oystertech.sportcms.model.form.AthleteForm;
import com.oystertech.sportcms.service.AthleteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 运动员接口
 *
 * @author Oyster Cheung
 * @version 2023-09-05
 * @since 2023-10-23
 */
@Tag(name = "运动员接口")
@RestController
@RequestMapping("/api/v2/athlete")
@RequiredArgsConstructor
public class AthleteController {
	private final AthleteService athleteService;

	@Operation(summary = "修改运动员", security = {@SecurityRequirement(name = "Authorization")})
	@PutMapping("/{id}")
	@PreAuthorize("@ss.hasPerm('athlete:edit')")
	public Result updateAthlete(
			@PathVariable Long id,
			@Valid @RequestBody AthleteForm athleteForm
	) {
		return Result.judge(athleteService.updateAthlete(id, athleteForm));
	}

	@Operation(summary = "修改运动员", security = {@SecurityRequirement(name = "Authorization")})
	@PostMapping()
	@PreAuthorize("@ss.hasPerm('athlete:add')")
	@PreventDuplicateSubmit
	public Result saveAthlete(
			@RequestBody AthleteForm athleteForm
	) {
		Assert.notNull(athleteForm.getBirthDate(), "请选择出生日期");
		Assert.notNull(athleteForm.getIdCard(), "请输入证件号");

		return Result.judge(athleteService.saveAthlete(athleteForm));
	}

	@GetMapping("/encIdCard/{idCardType}/{idCard}")
	public Result encIdCard(
			@PathVariable int idCardType,
			@PathVariable String idCard
	) {
		return Result.success(SecurityUtils.idCardEncrypt(idCardType, idCard));
	}

	@GetMapping("/decIdCard/{idCardType}/{idCardEnc}")
	public Result decIdCard(
			@PathVariable int idCardType,
			@PathVariable String idCardEnc
	) {
		return Result.success(SecurityUtils.idCardDecrypt(idCardType, idCardEnc));
	}
}
